#ifndef NET_SIGNAL_H_
#define NET_SIGNAL_H_

/*
 *  Sure: please start signal manager before start any other thread...
 *
 */

#include "base/base.h"
#include "net/poller.h"

namespace net {
typedef void (*SigCb)(void *ctx);
void Sig_handler(int fd, void* arg, int /*event*/);

struct Signal {
    int32 signo;
    SigCb cb;
    void* ctx;
};

class Poller;
class Event;
class SigManager {
  public:
    ~SigManager() {
      MapClear(&sig_map_);
    }

    Signal* RegSig(int32 signo);

    void Start();

    void HandleSignal(int32 signo);

    static SigManager* Instance(Poller* poller) {
      if (instance_ == NULL) {
        instance_ = new SigManager(poller);
      }

      return instance_;
    }

  private:
    Poller* poller_;
    Event sig_ev_;int32 sig_fd_;

    std::map<int, Signal*> sig_map_;

    explicit SigManager(Poller* poller);
    static SigManager* instance_;

    DISALLOW_COPY_AND_ASSIGN(SigManager);
};
}

#endif
